<template>
  <div class="credit">
    <my-tabs :tab-list="tabList"></my-tabs>
    <div class="credit-primary">
      <div class="credit-primary-son">
        <div class="title">我的积分</div>
        <el-row type="flex" align="middle">
          <span class="count">{{ credit }}</span>
          <img src="~@assets/images/finance/integral_big_icon.png" alt="" />
        </el-row>
      </div>
      <div class="credit-primary-module" v-for="(item, index) in creditModuleList" :key="index">
        <img :src="item.img" alt="" />
        <div class="title">{{ item.title }}</div>
        <div class="btn" @click="handleIncreaseWay(item.key)">{{ item.btn_text }}</div>
      </div>
    </div>
    <div class="credit-divider"></div>
    <div class="credit-list">
      <div class="title">积分明细</div>
      <el-table :data="creditList">
        <el-table-column label="积分来源">
          <template slot-scope="scope">
            {{ scope.row.note }}
          </template>
        </el-table-column>
        <el-table-column label="时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}
          </template>
        </el-table-column>
        <el-table-column label="积分" width="125">
          <template slot-scope="scope">
            {{ scope.row.alter_type ? '+' : '-' }}{{ scope.row.num }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <edu-pagination @handleCurrentChange="handleCurrentChange" align="right"></edu-pagination>

    <el-dialog :title="primaryTitle[primaryValue]" :visible.sync="dialogVisible" width="500px">
      <div class="credit-preview" v-for="item in primary" :key="item.key">
        <template v-if="primaryValue === item.key">
          <div class="text" v-for="(text, index) in item.content" :key="index">
            {{ text }}
          </div>
          <img class="img" :src="item.img" alt="" />
          <div class="btn" @click="dialogVisible = false">我知道了</div>
        </template>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import MyTabs from '../components/myTab.vue'
import EduPagination from '@/components/EduPagination'
import { mapState } from 'vuex'
export default {
  components: {
    MyTabs,
    EduPagination
  },
  data() {
    return {
      tabList: [
        {
          title: '我的积分'
        }
      ],
      creditModuleList: [
        {
          key: 'course',
          img: require('@assets/images/finance/integral_buy_icon.png'),
          title: '购买课程',
          btn_text: '购买赚积分'
        },
        {
          key: 'comment',
          img: require('@assets/images/finance/integral_comment_icon.png'),
          title: '评论课程',
          btn_text: '评论赚积分'
        },
        {
          key: 'share',
          img: require('@assets/images/finance/integral_share_icon.png'),
          title: '分享课程',
          btn_text: '分享赚积分'
        }
      ],
      creditList: [],
      dialogVisible: false,
      primary: [
        {
          key: 'comment',
          content: [
            '第一步：进入课程详情页或播放页',
            '第二步：切换至“评论”，在文本框内输入评论内容发布即可'
          ],
          img: require('@assets/images/finance/comment_img.png')
        },
        {
          key: 'share',
          content: [
            '第一步：进入课程详情页或播放页，点击分享图标',
            '第二步：点击分享海报底部的分享链接图标，分享完成即可'
          ],
          img: require('@assets/images/finance/share_img.png')
        }
      ],
      primaryValue: '',
      primaryTitle: {
        comment: '评论赚积分',
        share: '分享赚积分'
      }
    }
  },
  computed: {
    ...mapState({
      credit: state => state.my_integral.credit
    })
  },
  created() {
    this.handleIncomeInfo()
  },
  methods: {
    // 获取积分基础信息
    handleIncomeInfo() {
      this.$api.getCredit(
        {
          page: this.$store.getters['paginate/page'],
          count: 10
        },
        res => {
          const { data } = res
          this.$store.dispatch('my_integral/SET_INTEGRAL_INIT', {
            count: data.credit || 0,
            state: data.today_punch_in
          })
          this.creditList = data.flow.data
          this.$store.dispatch('paginate/setPaginate', {
            listRows: 10,
            total: data.flow.total
          })
        }
      )
    },
    // 分页
    handleCurrentChange() {
      this.handleIncomeInfo()
    },
    // 赚积分
    handleIncreaseWay(key) {
      if (key === 'course') {
        this.$router.push({ path: '/course' })
      } else {
        this.primaryValue = key
        this.dialogVisible = true
      }
    }
  },
  destroyed() {
    this.$store.commit('paginate/SET_PAGINATE_DEFAULT')
  }
}
</script>

<style lang="scss" scoped>
.credit {
  .credit-primary {
    display: flex;
    .credit-primary-son {
      width: 325px;
      height: 180px;
      padding-top: 49px;
      padding-left: 36px;
      box-sizing: border-box;
      background-image: url('~@assets/images/finance/integral_bg.png');
      background-repeat: no-repeat;
      .title {
        margin-bottom: 10px;
        font-size: 16px;
        font-family: SourceHanSansCN-Normal, SourceHanSansCN;
        font-weight: 400;
        color: #ffffff;
        line-height: 24px;
      }
      .count {
        font-size: 44px;
        font-family: DINPro-Bold, DINPro;
        font-weight: bold;
        color: #ffffff;
        line-height: 56px;
      }
    }
    .credit-primary-module {
      margin-left: 14px;
      flex-grow: 1;
      height: 180px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      border: 1px solid #dcdfe6;
      .title {
        margin-top: 12px;
        font-size: 16px;
        font-family: SourceHanSansCN-Medium, SourceHanSansCN;
        font-weight: 500;
        color: $text-color-import;
        line-height: 24px;
      }
      .btn {
        margin-top: 21px;
        padding: 7px 7.5px;
        font-size: 13px;
        font-family: SourceHanSansCN-Normal, SourceHanSansCN;
        font-weight: 400;
        color: #ffffff;
        background: linear-gradient(180deg, #ffa133 0%, #ff7b3d 100%);
        box-shadow: 0px 0px 12px 0px rgba(255, 121, 36, 0.4);
        border-radius: 15px;
        cursor: pointer;
      }
    }
  }
  .credit-divider {
    margin: 20px -20px 10px;
    height: 18px;
    background-color: $bg-color-grey;
  }
  .credit-list {
    ::v-deep .el-table th {
      background-color: #f6f9ff;
    }
    .title {
      margin-bottom: 16px;
      padding: 10px 0;
      font-size: 14px;
      font-family: SourceHanSansCN-Normal, SourceHanSansCN;
      font-weight: 400;
      color: $text-color-import;
      border-bottom: 1px solid #ebeef5;
    }
  }
  ::v-deep .el-dialog__header {
    border-bottom: 1px solid #ebeef5;
  }
  .credit-preview {
    .text {
      font-size: 14px;
      font-weight: 400;
      color: $text-color-import;
      line-height: 22px;
    }
    .img {
      width: 100%;
      display: block;
      margin-top: 10px;
    }
    .btn {
      margin: 38px auto 0;
      width: 94px;
      height: 30px;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
      line-height: 30px;
      text-align: center;
      background: #ff8a52;
      border-radius: 4px;
      cursor: pointer;
    }
  }
}
</style>
